<?php include("../../data/mysql.class.php")?>
<?php include("../../data/Function.php")?>
<?php
	$i=0;
    if(isset($_GET["Type"])){$Type=$_GET["Type"];}
    if(isset($_POST["Type"])){$Type=$_POST["Type"];}
	$conn=new Mysql();
	switch($Type){
		case "checkall":
		$sql = "select id,CatalogParentId,title from newscata_area  order by SortId asc";
		$arry=array();
            $rsrow=$conn->getAll($sql);
		foreach($rsrow as $k=>$rss){
			$arry[$i]["id"]=$rss["id"];
			$arry[$i]["pId"]=$rss["CatalogParentId"];
			$arry[$i]["name"]=$rss["title"];
			//if($i==0){$arry[$i]["open"]=true;}
			$arry[$i]["open"]=true;
			$i++;
			}
			echo json_encode($arry);
		break;
		case "add":
		$cat_add_name = trim($_POST["title"]);
		$doid=trim($_POST["sel_Sub"]);
		$xiaji = trim($_POST["xiaji"]);

		//$rsp_query = $conn->query("Select * from newscata_area where id=".$doid);
		$rsp =$conn->getAll("Select * from newscata_area where id=".$doid);
		if($rsp){
			$arootid = $rsp["rootid"];//==根ID号==
			$alevel = $rsp["Mlevel"];//==树深度==
		}
		else{
			$arootid = "";	//==根ID号==
			$alevel = 0;	//==树深度==
		}
			$alevel++;
			
		if($arootid ==""){
			$rsarootid = "*";
			$rsaMyrootid = ",*,";
		}
		else{
			$rsarootid = $arootid.",*";
			$rsaMyrootid = ",".$arootid.",*,";
		}

		$ins_sql = "INSERT INTO `newscata_area` (`title`, `CatalogParentId`, `Mlevel`, `SortId`, `rootid`, `MyRootid`,  `atime`, `leiid`, `xia`) VALUES ('$cat_add_name', '$doid', '$alevel', '1', '$rsarootid', '$rsaMyrootid', NOW(), NULL, $xiaji)";
		$menuadd = $conn->query($ins_sql);  //执行
			
			if($menuadd)
			{
				//echo "插入成功!";
			}
			else
			{
				echo $ins_sql;
				
			}			
			$gid = $conn->lastId(); //获取插入的ID号
			
		   // $showquery = $conn->query("select * from newscata_area order by id desc LIMIT 1");
			$rsU =$conn->getRow("select * from newscata_area order by id desc LIMIT 1");
			if($rsU)
			{
				$gidu = $rsU["id"];
				$groot = $rsU["rootid"];
				$c_groot = str_replace("*",$gidu,$groot);
				$rsUMyrootid = str_replace("*",$gidu,$rsU["MyRootid"]);
			}

			
			$menuupdate = $conn->query("update newscata_area set `rootid`='$c_groot',`MyRootid`='$rsUMyrootid',`SortId`='$gid' where `id`='$gid'");
			if($menuupdate)
			{
				echo 1;
			}
			else
			{
				echo 0;
				
			}
		break;
		case "delete":
			$TypeId=$_GET["id"];
			if($conn->query("delete from newscata_area where id in ($TypeId)")){echo 1;}else{echo 0;}

		break;
		case "edit":
		$ids = trim($_GET["uid"]);
		$cat_add_name = trim($_GET["title"]);
		$doid=trim($_GET["sel_Sub"]);
		$xiaji = trim($_GET["xiaji"]);
		$sql= "update `newscata_area` set title='$cat_add_name' where id= '$ids'";
		if($conn->query($sql)){
			echo 1;
			}else{
				echo $sql;
				}
		break;
		default:
		break;
		}
		
		
?>